<?php

class CrawlController extends Controller{

    private $M_news;
    private $M_categorys;
    private $M_sources;
    private $M_tags;
    private $M_taghasnews;
    private $M_categoryhastags;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_news = Model::getInstance('news');
        $this->M_categorys = Model::getInstance('categorys');
        $this->M_sources = Model::getInstance('sources');
        $this->M_tags = Model::getInstance('tags');
        $this->M_taghasnews = Model::getInstance('tag has news');
        $this->M_categoryhastags = Model::getInstance('category has tags');
    }

    //模块首页
    public function index(){
        global $controller;
        global $config_arr;

        $category_list = $this->M_categorys->getCategorys(null,true);
        $source_list = $this->M_sources->getSources(null,true);

        $data = array(
            'controller'=>$controller,
            'category_list'=>$category_list,
            'source_list'=>$source_list,
            'status_list'=>$config_arr['status']['name'],
        );

        Template::show('crawl/index.php',$data);

    }

    //获取来源
    public function ajaxGetSource(){
        $mode = $this->input()->get('mode',1);

        if($mode == 1){
            $source_list = $this->M_sources->getSources(null,false);
        }else if($mode == 2){
            $source_list = array(0=>array('source_id'=>4,'title'=>'36kr','site_address'=>'http://www.36kr.com'));
        }

        $data = array();
        if($source_list){
            foreach((array)$source_list as $k => $v){
                $data[$k][] = $v['source_id'];
                $data[$k][] = $v['title'];
            }
        }else{
            $data = array(array('all','请选择抓取来源'));
        }
        echo Response::Default_JSON(0,$data);
    }

    //单个资讯抓取
    public function getSigle(){
        $source = $this->input()->get('source');

        $source_info = $this->M_sources->get($source);
        $info = array();
        switch($source_info['title']){
            case '36kr':
                $info = $this->getSigleBy36kr();
                $info['source_id'] = $source;
                break;
        }

        echo Response::Default_JSON(200,$info);
    }

    //单个抓取下-》36kr.com 站点资讯抓取
    public function getSigleBy36kr(){
        set_time_limit(0);
        $p_id = $this->input()->get('p_id');

        $html = crawlHtml('http://www.36kr.com/p/'.$p_id);

        $info = array();
        $keywords_dom = $html->find('meta[name=keywords]', 0);
        $info['keywords'] =  $keywords_dom->content;

        $description_dom = $html->find('meta[name=description]', 0);
        $info['description'] =  $description_dom->content;
        $info['summary'] =  $description_dom->content;

        $title_dom = $html->find('h1.entry-title', 0);
        $info['title'] = $title_dom->plaintext;

        $content_dom = $html->find('div.mainContent',0);
        $content = $content_dom->innertext;

        $info['content'] = $content;

        $preImg = "/<[i|I][m|M][g|G].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/";
        preg_match_all($preImg, $content, $arr); //$content为原始文章内容，下同
        $info['s_picture'] = $arr[1];

        $tag = '';
        foreach ($html->find('a.tag') as $element){
            $tag .= !empty($tag) ? ',' . $element->plaintext : $element->plaintext;
        }

        $info['tag'] = $tag;

        $curr_category_dom = $html->find('span[itemprop=title]', 0);
        $info['curr_category'] = $curr_category_dom->plaintext;

        $info['time'] = date('Y-m-d H:i:s', strtotime($html->find('abbr.timeago',0)->title));

        $html->clear();

        return $info;
    }

    //批量资讯抓取
    public function getBatch(){
        $source = $this->input()->get('source','36kr');

        $info = array();
        switch($source){
            case '36kr':
                $info = $this->getBatchBy36kr();
                break;
        }

        echo Response::Default_JSON(200,$info);
    }

    //批量抓取下-》36kr.com 站点资讯抓取
    public function getBatchBy36kr(){
        set_time_limit(0);
        $type = $this->input()->get('type');
        $page = $this->input()->get('page');

        if(isset($type) && $type != null){
            $html = crawlHtml('http://www.36kr.com/category/'.$type.'?page='.$page);
            //$html = crawlHtml('http://www.36kr.com/recommended'.'?page='.$page);
            $arr = array();

            //$new_info_dom = $html->find('div.'.$type);
            $new_info_dom = $html->find('div.blogPost');

            foreach($new_info_dom as $k=>$v){

                $info = array();

                $info['href'] = 'http://www.36kr.com'.$v->find('div.left_info div.feature_img a',0)->href;
                $info['src'] = $v->find('div.left_info div.feature_img a img',0)->src;

                $info['title'] = $v->find('div.right_info div.summary h4 a',0)->plaintext;

                $info['type'] = $v->find('div.right_info div.post_meta a',1)->plaintext;
                $info['time'] = date('Y-m-d H:i:s', strtotime($v->find('div.right_info div.post_meta abbr',0)->title));
                $info['summary'] = $v->find('div.right_info div.excerpt p',0)->plaintext;

                $arr[$k] = $info;

            }

            $html->clear();

            return $arr;
        }
    }

    //单个新闻抓取入库
    public function ajaxSigleAddNew(){
        $category_id = $this->input()->post('category_id');
        $source_id = $this->input()->post('source_id');
        $title = $this->input()->post('title');
        $keywords = $this->input()->post('keywords');
        $description = $this->input()->post('description');
        $status = $this->input()->post('status');
        $summary = $this->input()->post('summary');
        $time = $this->input()->post('crawl_time');
        $content = $this->input()->post('content');
        $tag = $this->input()->post('tag','whoopsfeed new');
        $picture = $this->input()->post('picture');

        $published = $time;
        $update_time = $time;

        $content = content_preg_img($content,$time);

        $dir = DOCUMENT_ROOT_PATH. 'Upload/new/focus/'.date('Y',strtotime($time)).'/'.date('m',strtotime($time)).'/'.date('d',strtotime($time)).'/';
        if (!file_exists($dir)) {
            mkdirs($dir,0777);
        }
        $preImg = "/<[i|I][m|M][g|G].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/";

        $src_info = pathinfo($picture);
        $src_extension = explode('!',$src_info['extension']);

        $focus_pic = $dir.nameRule().'.'.$src_extension[0];

        if(!copy($picture.'!feature',$focus_pic)){
            echo Response::Default_JSON(201,array("msg" => "复制远程新闻图片失败，请联系管理员检查！"));
            exit;
        }
        $picture = explode('Upload', $focus_pic);

        $picture = '../Upload' . $picture[1];

        //去除 36氪站 新闻
        if(strpos($title,'36kr') || strpos($title,'氪') || strpos($content,'36kr') || strpos($content,'氪')){
            $status = 1;//待审核状态
        }else{
            $status = 3;//审核通过状态
        }

        $source_id = 4;
        //替换文章中图片路径
        preg_match_all($preImg, $content, $str_arr); //$content为原始文章内容，下同

        foreach ($str_arr[1] as $v) {

            $v_picture = explode('Upload', $v);

            $namePic[] = '../Upload' . $v_picture[1];
        }

        //替换所有图片链接为本站链接地址
        $new_content = str_replace($str_arr[1], $namePic, $content);


        $result = $this->M_news->addNew($category_id, $source_id, $title, $keywords, $description, $status, $summary, $new_content, $tag, $picture, rand(13698,13714), $published, $update_time);

        if($result){
            //标签 与 新闻 及 分类 的关联操作 $category_id 分类ID  $result 新闻ID
            $tag_arr = explode(',',$tag);

            foreach((array)$tag_arr as $k=>$v){

                $tag_id = $this->M_tags->addTag($v);
                if($tag_id){
                    //标签关联新闻
                    $this->M_taghasnews->addTagHasNews($tag_id,$result);
                    //分类关联标签
                    $this->M_categoryhastags->addCategoryHasTags($category_id,$tag_id);
                }
            }

            echo Response::Default_JSON(200,array("id" => $result));
        }else{
            echo Response::Default_JSON(201,array("msg" => "抓取插入失败"));
        }
    }

    //资讯入库
    public function ajaxAddNew(){
        set_time_limit(0);

        $p_id = $this->input()->get('p_id');
        $category = $this->input()->get('category');
        $title = $this->input()->get('title');
        $time = $this->input()->get('time');
        $summary = $this->input()->get('summary');
        $src = $this->input()->get('src');

        if(!is_numeric($p_id) || !$src){
            $data = array(
                "status"=>-3,
                "message"=> "抓取信信息不属于资讯！"
            );
            echo Response::Default_JSON(0,$data);
            exit;
        }

        $check_info = $this->M_news->getNewInfoByTitle($title);

        if($check_info != null){//资讯已存在
            echo Response::Default_JSON(0,array("status" => 2, "id" => $check_info['new_id']));
            exit;
        }

        //发布及更新时间
        $published = $time;
        $update_time= $time;

        $category_id = 0;
        $check_category = str_replace('创业','',$category);
        if($check_category == '国内公司'){
            $category_id = 1;
        }

        if($check_category == '国外公司'){
            $category_id = 2;
        }

        if(!in_array($category_id,array(1,2))){
            $category_info = $this->M_categorys->getCategoryInfoByName($category);
            $category_id = empty($category_info) ? 18 : $category_info['category_id'];
        }
        //信息抓取
        $html = crawlHtml('http://www.36kr.com/p/'.$p_id);

        $keywords_dom = $html->find('meta[name=keywords]', 0);
        $keywords =  $keywords_dom->content;

        $description_dom = $html->find('meta[name=description]', 0);
        $description =  $description_dom->content;

        if(!empty($summary)){
            $summary =  $summary;
        }else{
            $summary =  $description;
        }

        $content = '';
        foreach ($html->find('div.mainContent p') as $element){
            $content .= $element->outertext;
        }
        $content = content_preg_img($content,$time);

        $dir = DOCUMENT_ROOT_PATH. 'Upload/new/focus/'.date('Y',strtotime($time)).'/'.date('m',strtotime($time)).'/'.date('d',strtotime($time)).'/';
        if (!file_exists($dir)) {
            mkdirs($dir,0777);
        }
        $preImg = "/<[i|I][m|M][g|G].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/";
        if($src){
            $src_info = pathinfo($src);
            $src_extension = explode('!',$src_info['extension']);

            $focus_pic = $dir.nameRule().'.'.$src_extension[0];
            if(!copy($src,$focus_pic)){
                $data = array(
                    "status"=>-2,
                    "message"=> "复制远程新闻图片失败，请联系管理员检查！"
                );
                echo Response::Default_JSON(0,$data);
                exit;
            }
        }else{

            preg_match_all($preImg, $content, $arr); //$content为原始文章内容，下同

            $first_pic = $arr[1][0];    //获取资讯内第一张图片
            if(empty($first_pic)){
                $data = array(
                    "status"=>-2,
                    "message"=> "抓取新闻生成focus图片失败，请联系管理员检查！"
                );
                echo Response::Default_JSON(0,$data);
                exit;
            }

            $focus_pic = str_replace('big','focus',$first_pic);
            $retsult_status = imagezoom( $first_pic,$focus_pic, 320, 200, '#FFFFFF');

            if($retsult_status != 1){
                $data = array(
                    "status"=>-1,
                    "message"=> "抓取新闻生成图片失败，请联系管理员检查！"
                );
                echo Response::Default_JSON(0,$data);
                exit;
            }
        }

        $picture = explode('Upload', $focus_pic);
        $picture = '../Upload' . $picture[1];

        $tag = 'whoopsfeed new';

        foreach ($html->find('a.tag') as $element){
            $tag .= !empty($tag) ? ',' . $element->plaintext : $element->plaintext;
        }
        //去除 36氪站 新闻
        if(strpos($title,'36kr') || strpos($title,'氪') || strpos($content,'36kr') || strpos($content,'氪')){
            $status = 1;//待审核状态
        }else{
            $status = 3;//审核通过状态
        }

        $source_id = 4;
        //替换文章中图片路径
        preg_match_all($preImg, $content, $str_arr); //$content为原始文章内容，下同

        foreach ($str_arr[1] as $v) {

            $v_picture = explode('Upload', $v);

            $namePic[] = '../Upload' . $v_picture[1];
        }

        //替换所有图片链接为本站链接地址
        $new_content = str_replace($str_arr[1], $namePic, $content);

        $result = $this->M_news->addNew($category_id, $source_id, $title, $keywords, $description, $status, $summary, $new_content, $tag, $picture, rand(13698,13714), $published, $update_time);

        if($result){
            //标签 与 新闻 及 分类 的关联操作 $category_id 分类ID  $result 新闻ID
            $tag_arr = explode(',',$tag);

            foreach((array)$tag_arr as $k=>$v){
                $tag_id = $this->M_tags->addTag($v);

                if($tag_id){
                    //标签关联新闻
                    $this->M_taghasnews->addTagHasNews($tag_id,$result);
                    //分类关联标签
                    $this->M_categoryhastags->addCategoryHasTags($category_id,$tag_id);
                }
            }
        }

        echo Response::Default_JSON(0,array("status" => 1, "id" => $result));

        $html->clear();

        exit;
    }
}